/**
 *@description 用户画像》社交传播影响
 *@author yhw
 */
import {
  spreadQuery,
  spreadWeixinQuery,
  spreadContentQuery
} from '../services/SocialInfluence';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'SocialInfluence',

  state: {
    spreadData: null,
    spreadLoading: false,
    spreadWeixinData: null,
    spreadWeixinLoading: false,
    spreadContentData: null,
    spreadContentLoading: false,
  },

  subscriptions: {},

  effects: { * spreadQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'spreadLoading'
      });

      const {
        data
      } = yield call(spreadQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'spreadQuerySuccess',
          payload: {
            spreadData: temp,
          },
        });
      } else {
        error("获取用户粘度失败");
      }
    },
    //===================================
    * spreadWeixinQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'spreadWeixinLoading'
      });

      const {
        data
      } = yield call(spreadWeixinQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'spreadWeixinQuerySuccess',
          payload: {
            spreadWeixinData: temp,
          },
        });
      } else {
        error("获取互动活跃度失败");
      }
    },
    //===================================
    * spreadContentQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'spreadContentLoading'
      });

      const {
        data
      } = yield call(spreadContentQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'spreadContentQuerySuccess',
          payload: {
            spreadContentData: temp,
          },
        });
      } else {
        error("获取用户价值失败");
      }
    },

  },

  reducers: {
    spreadLoading(state) {
      return {...state,
        spreadLoading: true
      };
    },
    spreadQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        spreadLoading: false
      };
    },
    //===================================
    spreadWeixinLoading(state) {
      return {...state,
        spreadWeixinLoading: true
      };
    },
    spreadWeixinQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        spreadWeixinLoading: false
      };
    },
    //===================================
    spreadContentLoading(state) {
      return {...state,
        spreadContentLoading: true
      };
    },
    spreadContentQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        spreadContentLoading: false
      };
    },

  },
};